<?php
namespace app\index\controller;
use app\common\exception\System as SystemException;
use app\index\model\Video as Videos;
use app\index\model\Imgtext as News;
use app\index\model\Comment as Newscomment;
use app\index\model\Videocomment;
use app\index\model\Comment;
use think\Db;

class Video extends Common
{
    /**
   * 不需要登录
   * @var array
   */
  protected $noNeedLogin = ['*'];

  public function details(){
    $id = $this->request->param("id"); 
    $video=Videos::where("id",$id)->field("id,member_id,title,type,url,thumb")->find();
    $wh['news_id']=$id;
    $wh['comment_id']=0;
    $video['count']=Comment::where($wh)->count();

    $res=Comment::where([["news_id","=",$id],["type","=",2]])->field("id,news_id,member_id,parent_id,comment_id,content,create_time")->order("id asc")->select()->toArray();

    $Comment=$this->getTree($res);

    $news=News::field('id,member_id,title,thumb,create_time')->order("id asc")->limit(3)->select();
    

    if($video){
      $arr=['status'=>1,'video'=>$video,'Comment'=>$Comment,'news'=>$news,'msg'=>'获取成功'];
      // dump($arr);
      return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
    }else{
      $arr=['status'=>0,'msg'=>'获取失败'];
      return $this->successReturn($res,SystemException::E_SUCCESS, $arr['msg']);      
    }
  }
  public function getTree($data,$pid = 0){
      $tree = [];
      foreach($data as $value){
          if($value['comment_id'] == $pid){
              $value['child'] = $this->getTree($data,$value['id']);
              $tree[] = $value;
          }
      }
      return $tree;
  }
  public function add()
  {
    $data=$this->request->param();
    $data['member_id']=$this->auth->member_id;
    $model = new \app\index\model\Video;
    $res=$model->allowField(true)->save($data);
    if($res){
      $arr=['status'=>1,'msg'=>'提交成功'];
      return $this->successReturn($data,SystemException::E_SUCCESS, $arr['msg']); 
    }else{
      $arr=['status'=>0,'msg'=>'提交失败'];
      return $this->successReturn($res,SystemException::E_SUCCESS, $arr['msg']);      
    }
  }
  public function praise(){
  	$data=$this->request->param();
  	$data['member_id']=$this->auth->member_id;
    $res=db("news_praise")->where($data)->find(); 
    if($res){
    	$result=db("news_praise")->where($data)->delete();
    	if($result){
    		$arr=['status'=>1,'msg'=>'取消点赞'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}else{
		  	$arr=['status'=>3,'msg'=>'取消失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
    }else{
    	$data['create_time']=time();
    	$result=db("news_praise")->insert($data);
    	if($result){
    		$arr=['status'=>2,'msg'=>'点赞成功'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}else{
		  	$arr=['status'=>4,'msg'=>'点赞失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
    }
  }
  public function Collect(){
  	$data=$this->request->param();
  	$data['member_id']=$this->auth->member_id;
    $res=db("news_collect")->where($data)->find(); 
    if($res){
    	$result=db("news_collect")->where($data)->delete();
    	if($result){
    		$arr=['status'=>1,'msg'=>'取消收藏'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS,$arr['msg']); 
	  	}else{
		  	$arr=['status'=>3,'msg'=>'取消失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
    }else{
    	$data['create_time']=time();
    	$result=db("news_collect")->insert($data);
    	if($result){
    		$arr=['status'=>2,'msg'=>'收藏成功'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}else{
		  	$arr=['status'=>4,'msg'=>'收藏失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
    }
  }
  public function comments(){
  	$data=$this->request->param();
  	$data['member_id']=$this->auth->member_id;
  	$Comment=new Comment;
  	$res=$Comment->allowField(true)->save($data);
  	if($res){
    		$arr=['status'=>1,'msg'=>'评论成功'];
      		return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}else{
		  	$arr=['status'=>0,'msg'=>'评论失败'];
	      	return $this->successReturn($arr,SystemException::E_SUCCESS, $arr['msg']); 
	  	}
  }
}
